#!/bin/sh
#android 4.0以上和4.0以下方法不同
#get android sdk level
deviceId=$1
pid=$2

if [ $# != 2 ];then
    echo "sh get-android-net.sh deviceId pid";
    exit;
fi


currentDir=$(cd "$(dirname "$0")"; pwd)
echo "current dir : "$currentDir
datDir=$currentDir/../../../target/android-info/net/dat
htmlDir=$currentDir/../../../target/android-info/net/html

mkdir -p $datDir
mkdir -p $htmlDir

#获取pid
echo "根据pid获取uid"
echo "deviceId--"$deviceId
echo "pid--"$pid
uid="`adb -s $deviceId shell cat /proc/${pid}/status | grep Uid | grep -v PPid | grep -v Tra | awk '{print $2}'`"
echo "------------uid:"$uid

#apileveltemp=`adb shell getprop | grep ro.build.version.sdk`
#apilevel=${apileveltemp:25:2}
#chmod +x *.sh

#echo "android api level:"$apilevel
#if [ $apilevel -gt 14 ]
#then
#    ./get-android-net-gt-4.0.sh
#elif [ $apilevel -lt 14 ]
#then
#    ./get-android-net-lt-4.0.sh
#fi


while true
do
    #echo "get net info from /proc/net/xt_qtaguid/stats"
    ##第六列下载流量bytes，第八列上传流量tytes

    #echo "adb -s $deviceId shell cat /proc/net/xt_qtaguid/stats | grep $uid | awk '{print $6,$8}'"
    #echo $deviceId
    #echo $uid
    #pwd
    adb -s $deviceId shell cat /proc/net/xt_qtaguid/stats | grep $uid | awk '{print $6,$8}' > $datDir/one.dat

    txSum=0
    rxSum=0
    while read LINE
    do
        user=`echo $LINE`
        rx=`echo $user | awk '{print $1}'`
        #rxSum=$[$rxSum + $rx]
	rxSum=`expr $rxSum + $rx`        
#echo "rx" $rx

        tx=`echo $user | awk '{print $2}'`
        #txSum=$[$txSum + $tx]
        txSum=`expr $txSum + $tx`
	#echo "tx "$tx
    done < $datDir/one.dat
    echo $rxSum >> $datDir/rx.dat
    echo $txSum >> $datDir/tx.dat

    sleep 3

done
